package icegps.com.pingdj.activity;

import android.content.Intent;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import icegps.com.pingdj.App;
import icegps.com.pingdj.R;
import icegps.com.pingdj.bean.LocationStatus;
import icegps.com.pingdj.bean.PickerBean;
import icegps.com.pingdj.bean.WorkBean;
import icegps.com.pingdj.constant.ActionKey;
import icegps.com.pingdj.constant.Constant;
import icegps.com.pingdj.constant.Token;
import icegps.com.pingdj.data.ble.WriteHelper;
import icegps.com.pingdj.dialog.DialogPicker;
import icegps.com.pingdj.listener.OnUnitIndexListener;
import icegps.com.pingdj.utils.StringUtils;
import icegps.com.pingdj.utils.UnitUtils;
import icegps.com.pingdj.utils.Utils;
import icegps.com.pingdj.view.UnitTextView;
import icegps.com.pingdj.view.ValueTextView;

import static icegps.com.pingdj.constant.ActionKey.BPOS;
import static icegps.com.pingdj.constant.ActionKey.LAT;
import static icegps.com.pingdj.constant.ActionKey.LNG;
import static icegps.com.pingdj.constant.ActionKey.LOAD_TOKEN;
import static icegps.com.pingdj.constant.ActionKey.MEASURE_AVE_HEIGHT;
import static icegps.com.pingdj.constant.ActionKey.PLANE;
import static icegps.com.pingdj.constant.ActionKey.SAVE_TOKEN;
import static icegps.com.pingdj.constant.ActionKey.TITLE;

/**
 * Created by 111 on 2018/3/28.
 */

public class PlaneActivity extends BaseActivity implements OnUnitIndexListener {
    @BindView(R.id.tv_measure_ave_height)
    ValueTextView tvMeasureAveHeight;
    @BindView(R.id.tv_current_height)
    ValueTextView tvCurrentHeight;
    @BindView(R.id.tv_current_height_unit)
    UnitTextView tvCurrentHeightUnit;
    @BindView(R.id.tv_plane_set_height)
    ValueTextView tvPlaneSetHeight;
    @BindView(R.id.tv_plane_set_height_unit)
    UnitTextView tvPlaneSetHeightUnit;
    @BindView(R.id.ll_ave_set_height)
    LinearLayout llAveSetHeight;
    @BindView(R.id.tv_hint)
    TextView tvHint;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    private DialogPicker dialogPicker;
    private LocationStatus locationStatus;
    private WorkBean workBean;
    private boolean isFirst = true;
    private double measureAveHeight;
    private long loadToken;
    private long saveToken;


    @Override
    protected String setTitle() {
        return getString(R.string.do_plane);
    }

    @Override
    protected void init() {
        workBean = new WorkBean();
        getIntentExtra();
        initMeasureTv();
        setStartSetHeight(measureAveHeight);

        //准备作业
        WriteHelper.write(WriteHelper.GRADER + "1");
        initDialog();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        WriteHelper.write(WriteHelper.GRADER + "0");
    }


    private void initDialog() {
        dialogPicker = new DialogPicker(activity);
        dialogPicker.setOnUnitIndexListener(this);
    }

    private void initMeasureTv() {
        tvMeasureAveHeight.setText(UnitUtils.HEIGHT, deviceStatus.getHeight(), measureAveHeight);
    }

    private void getIntentExtra() {
        loadToken = getIntent().getLongExtra(LOAD_TOKEN, System.currentTimeMillis());
        saveToken = Token.CONTINUE_WORK_TOKEN;
        measureAveHeight = getIntent().getDoubleExtra(MEASURE_AVE_HEIGHT, Constant.measureAveHeight);
    }

    private void setStartSetHeight(double value) {
        tvPlaneSetHeight.setText(UnitUtils.HEIGHT, deviceStatus.getHeight(), value);
        tvPlaneSetHeight.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        tvPlaneSetHeight.getPaint().setAntiAlias(true);//抗锯齿
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_plane;
    }


    @OnClick({R.id.ll_ave_set_height, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_ave_set_height:
                setAveSetHeight();
                break;
            case R.id.tv_confirm:
                float currentHight = Float.parseFloat(tvCurrentHeight.getText().toString());
                float planeSetHeight = Float.parseFloat(tvPlaneSetHeight.getText().toString());
                if (Math.abs(currentHight - planeSetHeight) > 10) {
                    Utils.showToast(getString(R.string.not_more_than_ten_meters));
                    return;
                }
                startWork();
                break;
        }
    }


    private void startWork() {
        if (locationStatus == null) {
            Utils.showToast(getString(R.string.location_status_do_no_user));
            return;
        }
        if (status!=4){
            Utils.showToast(getString(R.string.location_status_do_no_user));
            return;
        }
        workBean
                .setId(saveToken)
                .setWorkAction(2)
                .setType(PLANE)
                .setTime(System.currentTimeMillis())
                .setStartLongitude(locationStatus.getLongitude())
                .setStartLatitude(locationStatus.getLatitude())
                .setStartSetHeight(Float.parseFloat(tvPlaneSetHeight.getText().toString()))
                .setEndLongitude(locationStatus.getLongitude())
                .setEndLatitude(locationStatus.getLatitude())
                .setEndSetHeight(Float.parseFloat(tvPlaneSetHeight.getText().toString()))
                .setOffset(0).toString();

        WriteHelper.write(WriteHelper.GRADER + workBean.toString());

        App.getInstance().getDaoSession().getWorkBeanDao().insertOrReplace(workBean);
        Intent intent = new Intent(activity, WorkMapActivity.class);
        intent.putExtra(LNG, workBean.getStartLongitude());
        intent.putExtra(LAT, workBean.getStartLatitude());
        intent.putExtra(TITLE, getString(R.string.do_plane));
        intent.putExtra(LOAD_TOKEN, loadToken);
        intent.putExtra(SAVE_TOKEN, saveToken);
        startActivity(intent);
        finish();
    }

    private void setAveSetHeight() {
        int[] indexs = new int[10];
        String s = tvPlaneSetHeight.getText().toString();
        if (s.contains("-")) {
            indexs[0] = 1;
            s = s.replace("-", "");
        } else {
            indexs[0] = 0;
        }
        for (int i = 0; i < s.length(); i++) {
            String ss = String.valueOf(s.charAt(s.length() - 1 - i));
            if (ss.equals(".")) {
                indexs[indexs.length - 2 - i] = 0;
            } else {
                indexs[indexs.length - 2 - i] = Integer.parseInt(ss);
            }
        }
        ArrayList<PickerBean> arrayList = new ArrayList<>();
        String[] symbol = {"+", "-"};
        String[] values = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
        PickerBean p1 = new PickerBean(symbol, DialogPicker.PICKER_VIEW, indexs[0]);
        PickerBean p2 = new PickerBean(values, DialogPicker.PICKER_VIEW, indexs[1]);
        PickerBean p3 = new PickerBean(values, DialogPicker.PICKER_VIEW, indexs[2]);
        PickerBean p4 = new PickerBean(values, DialogPicker.PICKER_VIEW, indexs[3]);
        PickerBean p5 = new PickerBean(values, DialogPicker.PICKER_VIEW, indexs[4]);
        PickerBean p6 = new PickerBean(new String[]{"."}, DialogPicker.TEXT_VIEW, indexs[5]);
        PickerBean p7 = new PickerBean(values, DialogPicker.PICKER_VIEW, indexs[6]);
        PickerBean p8 = new PickerBean(values, DialogPicker.PICKER_VIEW, indexs[7]);
        PickerBean p9 = new PickerBean(values, DialogPicker.PICKER_VIEW, indexs[8]);
        PickerBean p10 = new PickerBean(new String[]{tvPlaneSetHeightUnit.getText().toString()}, DialogPicker.TEXT_VIEW, indexs[9]);
        arrayList.add(p1);
        arrayList.add(p2);
        arrayList.add(p3);
        arrayList.add(p4);
        arrayList.add(p5);
        arrayList.add(p6);
        arrayList.add(p7);
        arrayList.add(p8);
        arrayList.add(p9);
        arrayList.add(p10);

        dialogPicker.setLayoutOrder(arrayList, llAveSetHeight.getId());
        dialogPicker.show();
    }

    @Override
    public void onUnitIndex(int viewId, int[] selindex, String[] values) {
        String value = "";
        for (String s : values) {
            value += s;
        }
        if (TextUtils.isEmpty(value)) value = "0";
        float antennaLocation = Float.parseFloat(value);
        WriteHelper.write(WriteHelper.SET_ANTENNAP + value);
        tvPlaneSetHeight.setText(StringUtils.setAccuracy(antennaLocation, 3));
    }

    @Override
    public void onAnalysisHexLocationData(LocationStatus locationStatus) {
        super.onAnalysisHexLocationData(locationStatus);
        this.locationStatus = locationStatus;
        tvCurrentHeight.setText(StringUtils.setAccuracy(locationStatus.getAltitude(), 3));
        if (measureAveHeight == 0 && isFirst) {
            isFirst = false;
            setStartSetHeight(locationStatus.getAltitude());
        }
    }

}
